﻿#Include ..\baseHandle.ahk

class RandomGen extends BaseHandle {

  static Handle(parsed) {
    charset := '0123456789abcdefghijklmnopqrstuvwxyz'
    for v in parsed.params {
      switch v {
        case 'W': charset .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        case 'u': charset .= '_'
      }
    }
    prefix := parsed.kvparams.HasProp('p') ? parsed.kvparams.p : ''
    loop parsed.target - (prefix ? prefix.Length : 0)
      s .= charset[Random(1, charset.Length)]
    return this.Succ(prefix s)
  }

    static Spec() {
    return {
      cmd: 'r',
      desc: '生成指定长度的随机串',
      format: 'r [-u|W] [-p=?] <target>',
      params: [{
        name: 'u',
        desc: '使用下划线'
      }, {
        name: 'W',
        desc: '使用大写字母'
      }],
      kvparams: [{
        name: 'p',
        desc: '指定前缀'
      }],
      args: {
        desc: '生成随机串的长度',
        isOptional: false,
        cacheable: false,
      },
      extra: {
        isEnable: false
      }
    }
  }
}
